var db = require("./libs/db");
var rtc = require("./libs/rtc");
var mqclient = require("./libs/mqclient");
var webSvr = require("./libs/webSvr");
var mediaServer = require("./libs/mediaStream");
var kmsPan = require("./libs/kmsPan");


var appObjs = {
    db: new db.TDBMySql(),
    rtc: new rtc.TRtcMonitor(),
    mqClient: new mqclient.TMqClient(),
    webSvr: new webSvr.TWebSvr(),
    httpSvr: new webSvr.TWebSvr(),
    mediaServer: new mediaServer.TMediaStream(),
    kmsPan: new kmsPan.TKmsSvr(),
    start() {
        var me = appObjs;
        return new Promise((success, faild) => {
            try {
                me.initDB().then(_ => {
                    return me.openMqClient();
                }).then(_ => {
                    return me.loadMediaServer();
                }).then(_ => {
                    return me.startWeb();
                }).then(_ => {
                    return me.kmsPan.loadTaskObjs();
                }).then(_ => {
                    me.kmsPan.regist2WebSvr(me.webSvr);
                    success();
                });
            } catch (er) {
                console.log(er);
                success();
            }
        })
    },
    initDB() {
        var me = appObjs;
        return new Promise((success, faild) => {
            try {
                me.db.setConfig('hhKmsV10', {
                    host: "tjiot.hhdata.cn",
                    port: 53306,
                    uid: "root",
                    pwd: "hhuc1115",
                    db: "hhKmsV10",
                    sqlPath: "D:/project/dataAmbulance/data-ambulance/client/kmsv10/sqls"
                });
                me.db.regist2WebSvr(me.webSvr);
                success();
            } catch (er) {
                console.log(er);
                success();
            }
        })
    },
    openMqClient() {
        var me = appObjs;
        return new Promise((success, faild) => {
            try {
                //set events
                //subscribe topics
                me.mqClient.open().then(_ => {
                    success();
                })
            } catch (er) {
                console.log(er);
                success();
            }
        })
    },
    initRtc() {
        var me = appObjs;
        return new Promise((success, faild) => {
            try {
                me.rtc.regist2WebSvr(me.webSvr)
                me.rtc.scanObjs().then(_ => {
                    me.rtc.start();
                    success();
                });
            } catch (er) {
                console.log(er);
                success();
            }
        })
    },
    loadMediaServer() {
        var me = appObjs;
        return new Promise((success, faild) => {
            try {
                me.mediaServer.regist2WebSvr(me.webSvr);
                me.mediaServer.regist2WebSvr(me.httpSvr);
            } catch (er) {
                console.log(er);
            }
            success();
        })
    },
    startWeb() {
        var me = appObjs;
        return new Promise((success, faild) => {
            try {
                me.webSvr.statics['/web'] = __dirname + "/static";
                me.webSvr.start().then(_ => {
                    me.httpSvr.config.port = 20040;
                    me.httpSvr.config.https.enable = false;
                    return me.httpSvr.start();
                }).then(_ => {
                    success();
                })
            } catch (er) {
                console.log(er);
                success();
            }
        })
    }
}

appObjs.start().then(_ => {
    console.log(' start system. ')
})